<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script>

    // "http://127.0.0.1:5500/day13/html/login.html?user=a123123&pwd=123123&phone=17386141516&email=1272071496%40qq.com"

    // 参数数据"?user=a123123&pwd=123123&phone=17386141516&email=1272071496%40qq.com"

    // 将参数数据解析为对象 => {user:"xxx",pwd:"xxx"}
    // 1. 先用 & 将字符串拆分为 数组  =>  ['user=a123123', 'pwd=123123', 'phone=17386141555', 'email=123123123%40qq.com']
    // 2. 遍历数组 将每个键值对拆分为 键名 键值 -> 存到对象中

    var str = "user=a123123&pwd=123123&phone=17386141555&email=123123123@qq.com";
    // console.log(str);
    var list = str.split("&");
    // console.log(list);  // ['user=a123123', 'pwd=123123', 'phone=17386141555', 'email=123123123%40qq.com'

    var data = {};
    for (var i = 0; i < list.length; i++) {
        var item = list[i];  // 每一条数据 'user=a123123'
        var key = item.split("=")[0]; // "user"
        var val = item.split("=")[1]; // "a123123"
        // console.log(item, key, val);

        // data[key] = val;
        data[key] = decodeURIComponent(val); // 对键值进行解码

    }

    console.log(data);


    // 将对象解析为参数数据队列
    // 1. 遍历对象 -> 获取键名和键值 -> 拼接单条参数数据 "user=a123123" 放到数组中
    // 2. 将数组中的元素 用& 拼接


    var list = [];
    for (var key in data) {
        var val = data[key];
        list.push(`${key}=${val}`);
    }
    console.log(list);
    data = list.join("&");
    console.log(data);

</script>

</html>